using Abc.Core.Entities;

using Furion.DatabaseAccessor;
using Furion.DependencyInjection;

using Mapster;

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

using System.Linq;
using System.Threading.Tasks;

using Tool;

namespace Abc.Application
{
    /// <summary>
    /// 短信发送记录
    /// </summary>
    [ApiDescriptionSettings(Name = "SmsSend")]
    //[Injection(Proxy = typeof(LogDispatchProxy))]
    [ServiceFilter(typeof(ApiActionFilterAttribute))]
    public class SmsSendService : ISmsSendService, ITransient
    {
        private readonly ILogger<SmsSendService> _logger;
        private readonly IRepository<SmsSend> _smsSendRepository;

        //private readonly IMemoryCache _memoryCache;
        public SmsSendService(ILogger<SmsSendService> logger
        //, IMemoryCache memoryCache
        , IRepository<SmsSend> smsSendRepository
         )
        {
            _logger = logger;
            // _memoryCache = memoryCache;
            //_memoryCache =  App.GetService<IMemoryCache>();
            _smsSendRepository = smsSendRepository;
        }

        /// <summary>
        /// 新增或更新一条
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ResultData<string>> InsertOrUpdate(SmsSendInput input)
        {
            var resultdata = new ResultData<string>();
            if (input.Id > 0)
            {
                var dbentity = await _smsSendRepository.FindAsync(input.Id);
                input.Adapt(dbentity);
                await _smsSendRepository.UpdateAsync(dbentity);
            }
            else
            {
                var newEntity = await _smsSendRepository.InsertAsync(input.Adapt<SmsSend>());
            }
            resultdata.Succeeded = true;
            resultdata.Errors = "成功";
            return resultdata;
        }

        /// <summary>
        /// 删除一条
        /// </summary>
        /// <param name="id"></param>
        public async Task<ResultData<string>> Delete(int id)
        {
            var resultdata = new ResultData<string>();
            await _smsSendRepository.DeleteAsync(id);

            resultdata.Succeeded = true;
            resultdata.Errors = "成功";
            return resultdata;
        }

        /// <summary>
        /// 查询一条[跟踪实体]
        /// </summary>
        /// <param name="id"></param>
        public async Task<SmsSendOut> Find(int id)
        {
            var dbentity = await _smsSendRepository.FindAsync(id);
            return dbentity.Adapt<SmsSendOut>();
        }

        /// <summary>
        /// 查询一条[跟踪实体]
        /// </summary>
        /// <param name="id"></param>
        public async Task<SmsSendOut> Get(int id)
        {
            var dbentity = await _smsSendRepository.FirstOrDefaultAsync(o => o.Id == id, tracking: false);
            if (dbentity == null) return null;
            return dbentity.Adapt<SmsSendOut>();
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="searchQuery"></param>
        /// <returns></returns>
        public async Task<PagedListToLayui<SmsSendOut>> GetAllByPageToLayui(SmsSendQuery searchQuery)
        {
            //var queryable= _smsSendRepository.DetachedEntities.AsQueryable();
            //if (!string.IsNullOrWhiteSpace(searchQuery.KeyWords))
            //{
            //    var kw = searchQuery.KeyWords.ToLower();
            //    queryable = queryable.Where(o => o.No.Contains(kw));
            //}

            var pageResult = _smsSendRepository.DetachedEntities.AsQueryable().OrderByDescending(o => o.Id).ProjectToType<SmsSendOut>();
            return await pageResult.ToPagedListToLayuiAsync(searchQuery.Page, searchQuery.Limit);
        }

        #region 默认未启用

        ///// <summary>
        ///// 新增一条
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //public async Task<ResultData<string>> Insert(SmsSendInput input)
        //{
        //    var resultdata = new ResultData<string>();
        //    var newEntity = await _smsSendRepository.InsertAsync(input.Adapt<SmsSend>());
        //
        //    resultdata.Succeeded = true;
        //    resultdata.Errors = "成功";
        //    return resultdata;
        //}
        //
        ///// <summary>
        ///// 更新一条
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //public async Task<ResultData<string>> Update(int id,SmsSendInput input)
        //{
        //    var resultdata=new ResultData<string>();
        //    var dbentity = await _smsSendRepository.FindAsync(id);
        //    input.Adapt(dbentity);
        //    await _smsSendRepository.UpdateAsync(dbentity);
        //
        //    resultdata.Succeeded = true;
        //    resultdata.Errors = "成功";
        //    return resultdata;
        //}
        //
        ///// <summary>
        ///// 新增或更新多条
        ///// </summary>
        ///// <param name="inputs"></param>
        ///// <returns></returns>
        //public async Task<ResultData<string>> Inserts(List<SmsSendInput> inputs)
        //{
        //    var resultdata = new ResultData<string>();
        //    var dbaddinputs = inputs.Adapt<List<SmsSend>>();
        //    await _smsSendRepository.InsertAsync(dbaddinputs);
        //    resultdata.Succeeded = true;
        //    resultdata.Errors = "成功";
        //    return resultdata;
        //}
        ///// <summary>
        ///// 新增或更新多条
        ///// </summary>
        ///// <param name="inputs"></param>
        ///// <returns></returns>
        //public async Task<ResultData<string>> Updates(List<SmsSendInput> inputs)
        //{
        //    var resultdata = new ResultData<string>();
        //    var ids = inputs.Select(o => o.Id).ToList();
        //    var dbentitys = await _smsSendRepository.Where(u => ids.Any(a => a == u.Id)).ToListAsync();
        //    inputs.Adapt(dbentitys);
        //    await _smsSendRepository.UpdateAsync(dbentitys);
        //
        //    resultdata.Succeeded = true;
        //    resultdata.Errors = "成功";
        //    return resultdata;
        //}
        //
        ///// <summary>
        ///// 新增或更新多条
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //public async Task<ResultData<string>> InsertOrUpdates(List<SmsSendInput> inputs)
        //{
        //    var resultdata = new ResultData<string>();
        //    if (inputs.Any(o => o.Id > 0))
        //    {
        //        var updates = inputs.Where(o => o.Id > 0);
        //        var ids = updates.Select(o=>o.Id).ToList();
        //        var dbentity =  _smsSendRepository.Where(o=> ids.Any(a=>a==o.Id));
        //        inputs.Adapt(dbentity);
        //        await  _smsSendRepository.UpdateAsync(dbentity);
        //    }
        //    else
        //    {
        //       var adds = inputs.Where(o => o.Id <= 0);
        //       await _smsSendRepository.InsertAsync(adds.Adapt<List<SmsSend>>());
        //
        //    }
        //    resultdata.Succeeded = true;
        //    resultdata.Errors = "成功";
        //    return resultdata;
        //}
        //
        //
        ///// <summary>
        ///// 查询所有
        ///// </summary>
        ///// <returns></returns>
        //public async Task<List<SmsSendDto>> GetAll()
        //{
        //    var entities = _smsSendRepository.DetachedEntities.AsQueryable().ProjectToType<SmsSendDto>();
        //    var datas= await entities.ToListAsync();
        //    datas.SetMemberCache("Table_SmsSend");
        //    return datas;
        //}
        ///// <summary>
        ///// 分页查询
        ///// </summary>
        ///// <param name="searchQuery"></param>
        ///// <returns></returns>
        //public async Task<PagedList<SmsSendDto>> GetAllByPage(SmsSendQuery searchQuery)
        //{
        //    //var queryable= _smsSendRepository.DetachedEntities.AsQueryable(tracking:false);
        //    //if (!string.IsNullOrWhiteSpace(searchQuery.KeyWords))
        //    //{
        //    //    var kw = searchQuery.KeyWords.ToLower();
        //    //    queryable = queryable.Where(o => o.No.Contains(kw));
        //    //}
        //    var pageResult = _smsSendRepository.DetachedEntities.AsQueryable().OrderByDescending(o => o.Id).ProjectToType<SmsSendDto>();
        //
        //    return await pageResult.ToPagedListAsync(searchQuery.Page, searchQuery.Limit);
        //}
        //
        //
        //
        //
        ///// <summary>
        ///// 设置状态
        ///// </summary>
        ///// <param name="id"></param>
        ///// <param name="state"></param>
        ///// <param name="field">字段名</param>
        ///// <returns></returns>
        //public async Task<ResultData<string>> SetState(int id, bool state, string field)
        //{
        //    var resultdata = new ResultData<string>();
        //    var entity = await _smsSendRepository.FirstOrDefaultAsync(o => o.Id == id);
        //
        //    switch (field.ToLower())
        //    {
        //
        //
        //
        //
        //
        //
        //
        //
        //
        //        case  "isuse" :
        //            {
        //                entity.IsUse = state;
        //            }
        //            break;
        //
        //
        //
        //
        //
        //
        //
        //
        //
        //
        //        case  "isdeleted" :
        //            {
        //                entity.IsDeleted = state;
        //            }
        //            break;
        //
        //
        //
        //
        //        default:
        //            break;
        //    }
        //
        //    resultdata.Succeeded = true;
        //    resultdata.Errors = "成功";
        //    return resultdata;
        //}

        #endregion 默认未启用
    }

    public interface ISmsSendService
    {
        /// <summary>
        /// 新增或更新一条
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        Task<ResultData<string>> InsertOrUpdate(SmsSendInput input);

        /// <summary>
        /// 删除一条
        /// </summary>
        /// <param name="id"></param>
        Task<ResultData<string>> Delete(int id);

        /// <summary>
        /// 查询一条
        /// </summary>
        /// <param name="id"></param>
        Task<SmsSendOut> Find(int id);

        /// <summary>
        /// 查询一条
        /// </summary>
        /// <param name="id"></param>
        Task<SmsSendOut> Get(int id);

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="searchQuery"></param>
        /// <returns></returns>
        Task<PagedListToLayui<SmsSendOut>> GetAllByPageToLayui(SmsSendQuery searchQuery);

        #region 默认未启用

        ///// <summary>
        ///// 新增一条
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //Task<ResultData<string>> Insert(SmsSendInput input);
        //
        ///// <summary>
        ///// 更新一条
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //Task<ResultData<string>> Update(int id,SmsSendInput input);
        ///// <summary>
        ///// 新增或更新一条
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //Task<ResultData<string>> Inserts(List<SmsSendInput> inputs);
        ///// <summary>
        ///// 新增或更新多条
        ///// </summary>
        ///// <param name="inputs"></param>
        ///// <returns></returns>
        //Task<ResultData<string>> Updates(List<SmsSendInput> inputs);
        ///// <summary>
        ///// 新增或更新多条
        ///// </summary>
        ///// <param name="inputs"></param>
        ///// <returns></returns>
        //Task<ResultData<string>> InsertOrUpdates(List<SmsSendInput> inputs);
        ///// <summary>
        ///// 查询所有
        ///// </summary>
        ///// <returns></returns>
        //Task<List<SmsSendDto>> GetAll();
        ///// <summary>
        ///// 分页查询
        ///// </summary>
        ///// <param name="searchQuery"></param>
        ///// <returns></returns>
        //Task<PagedList<SmsSendDto>> GetAllByPage(SmsSendQuery searchQuery);
        ///// <summary>
        ///// 设置状态
        ///// </summary>
        ///// <param name="id"></param>
        ///// <param name="state"></param>
        ///// <param name="field">字段名</param>
        ///// <returns></returns>
        //Task<ResultData<string>> SetState(int id, bool state, string field);

        #endregion 默认未启用
    }
}